package com.caifu.mapper;

import com.caifu.pojo.ZcOrderingRecords;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 订餐记录表 Mapper 接口
 * </p>
 *
 * @author szt
 * @since 2020-11-09
 */
public interface ZcOrderingRecordsMapper extends BaseMapper<ZcOrderingRecords> {

    @Select("select * from sys_organization where org_id = #{orgId}")
    Map<String,String> getOrgInfo(@Param("orgId") Long orgId);

    @Select("select a.id,a.user_no,a.user_type,b.user_name as zd_user_name,c.user_name as fd_user_name,b.user_sex as zd_user_sex,c.sex as fd_user_sex,b.birth_date as zd_birth_date,DATE_FORMAT(c.brith_date,'%Y-%m-%d') as fd_birth_date,a.days,a.days_count,a.subsidy,a.meals,a.delivery_fee,a.cost,a.type,a.address from zc_ordering_records a left join user_info b on a.user_no = b.user_no left join zc_no_archives_user c on a.user_no = c.user_no where a.status <> '9' and (b.status = '1' or b.status is null or b.status = '') and a.order_id = #{orderId} and a.month = #{month} and a.food_type = #{foodType} order by a.user_type,b.user_name,c.user_name,a.user_no")
    List<Map<String,String>> getOrderData(@Param("orderId")String orderId,@Param("month") String month,@Param("foodType") String foodType);

    @Select("select count(*) from zc_ordering_records a left join zc_ordering b on a.order_id = b.id where a.status = '1' and b.status <> '9' and b.org_id = #{orgId} and a.month = #{month} and a.food_type = #{foodType}")
    int getOrderingCount(@Param("orgId") String orgId,@Param("month") String month,@Param("foodType") String foodType);

    @Select("select d.org_name,b.user_type,e.user_name as zd_user_name,f.user_name as fd_user_name,e.birth_date as zd_birth_date,f.brith_date as fd_birth_date,e.user_sex as zd_sex,f.sex as fd_sex,b.subsidy,b.meals,b.delivery_fee,b.cost,b.days,b.days_count,b.type,b.food_type from zc_ordering_records b left join zc_ordering c on b.order_id = c.id left join sys_organization d on c.org_id = d.org_id left join user_info e on b.user_no = e.user_no left join zc_no_archives_user f on b.user_no = f.user_no where c.status = '3' and b.status = '1' and b.user_type ='1' and c.org_id = #{orgId} and c.month = #{month} and b.food_type = #{type} order by b.user_type,e.user_name,f.user_name,b.user_no")
    List<Map<String, String>> getExportData(@Param("orgId") String orgId,@Param("month") String month,@Param("type") String type);

    @Select("select a.type,count(a.id) as count,a.confirm_status from zc_ordering_record_details a left join zc_ordering_records b on a.record_id = b.id left join zc_ordering c on b.order_id = c.id where c.status = '3' and b.status = '1' and c.org_id = #{orgId} and a.month = #{month} and a.day = #{day} group by a.type,a.confirm_status order by a.type")
    List<Map<String,String>> getFoodType(@Param("orgId") String orgId,@Param("month") String month,@Param("day") String day);

    @Update("update zc_ordering_record_details set confirm_status = '2',confirm_time = #{now} where id in(select t.id from (select a.id from zc_ordering_record_details a left join zc_ordering_records b on a.record_id = b.id left join zc_ordering c on b.order_id = c.id where c.org_id = #{orgId} and a.month = #{month} and a.day = #{day} and a.type = #{type}) as t)")
    void confirm(@Param("orgId") String orgId, @Param("month") String month, @Param("day") String day, @Param("type") String type, @Param("now") Date now);

    @Select("select count(a.id) as count from zc_ordering_record_details a left join zc_ordering_records b on a.record_id = b.id left join zc_ordering c on b.order_id = c.id where c.org_id = #{orgId} and a.month = #{month} and a.day = #{day} and a.confirm_status = '1'")
    int getNoConfirmCount(@Param("orgId") String orgId, @Param("month") String month, @Param("day") String day);
}
